package uiLayer;

import java.io.IOException;
import java.util.Scanner;

import modelLayer.ProductRecords;
import modelLayer.SLIRecords;
import modelLayer.SaleRecords;

import controllerLayer.CustomerCtr;
import controllerLayer.EmployeeCtr;
import controllerLayer.LoanCtr;
import controllerLayer.ProductCtr;
import controllerLayer.SaleCtr;



public class OfficeUI {

	private static OfficeUI instance = null;
	private static EmployeeCtr ectr;
	private static CustomerCtr cctr;
	private static ProductCtr pctr;
	private static LoanCtr lctr;
	private static SaleCtr sctr;

	public OfficeUI() {
		ectr = new EmployeeCtr();
		cctr = new CustomerCtr();
		pctr = new ProductCtr();
		lctr = new LoanCtr();
		sctr = new SaleCtr();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			load();
		SaveToFile.getInstance().clearFile();
		OfficeUI instance = new OfficeUI();
		instance.run();
		}
		catch(Exception e){
			saveToFile();
			System.out.println("Unknown error, contact the administrator.");
		}
		
	}
	public static OfficeUI getInstance()       
	{
		if(instance == null)
		{
			instance = new OfficeUI();
		}
		return instance;
	}
	
		

	public void run()
	{
		
		boolean exit = false;
		while(!exit){
		int choice = printOfficeMenu();
		switch(choice)
		{
		 case 1: runCustomersMenu();
         break;
		 case 2: runEmployeesMenu();
         break;
		 case 3: runSalesMenu();
         break;
		 case 4: runLeaseMenu();
		 break;
		 case 5: runContractorMenu();
		 break;
		 case 6: runTimberUI();
		 break;
		 case 7: runDIYUI();
		 break;
		 case 8: goBack();
         break;
		}
		
		}
	}
	public static void saveToFile() {
		try {
			pctr.saveProd();
			ectr.save();
			cctr.save();
			pctr.saveContr();
			pctr.saveSLI();
			sctr.save();
			lctr.save();
		} 
		catch (IOException|NullPointerException e) {
			e.printStackTrace();
			System.out.println("Loading error");
		}			
		}
	public static void load(){
		InputParser inp = InputParser.getInstance();
		inp.load();
		SaleRecords.getInstance().setId(SaleRecords.getInstance().getOffers().size());
		ProductRecords.getInstance().setUniqueId(ProductRecords.getInstance().getProducts().size());
		SLIRecords.getInstance().setCollectionId(SLIRecords.getInstance().getCollections().size());
	}
	private void runDIYUI() {
		DIYUI.getInstance().run();
		
	}
	private void runTimberUI() {
		TimberUI.getInstance().run();
		
	}
	private void runContractorMenu() {
		System.out.print('\f');
		 ContractorUI.getInstance().contractorMenuStart();
		
	}
	private void goBack() {
		saveToFile();
		System.exit(1);
		
	}
	
private void runLeaseMenu() {
	System.out.print('\f');
	 ProductsForLeaseUI.getInstance().loanMenuStart();
		
	}

private void runSalesMenu() {
	System.out.print('\f');
	 ProductUI.getInstance().productMenuStart();
		
	}

private void runEmployeesMenu() {
	 System.out.print('\f');
	 EmployeeUI.getInstance().employeeMenuStart();
		
	}

private void runCustomersMenu() {
	 System.out.print('\f');
	 CustomerUI.getInstance().customerMenuStart();
		
	}

public int printOfficeMenu(){
	  @SuppressWarnings("resource")
	Scanner keyboard = new Scanner(System.in);
      //System.out.print('\f');
      System.out.println("***Office Menu***");
      System.out.println("1. Customers");
      System.out.println("2. Employees");
      System.out.println("3. Products For Sale");
      System.out.println("4. Products For Lease");
      System.out.println("5. Contractors");
      System.out.println("6. Timber");
      System.out.println("7. DIY");
      System.out.println("8. Quit");
      System.out.println("Choice #: ");
      
      int choice = InputParser.getInstance().parseInt(keyboard.nextLine());
      return choice;
}
}
